import 'package:sqflite/sqlitex.dart';

@SqlitexDatabase(tables: [OfflineTable, Offline33Table])
// flutter pub run build_runner build
class AppDatabase extends SqliteDataBase {

  static AppDatabase? _instance;

  static Future<AppDatabase> getInstance() async {
    // if (_instance != null) return _instance!;
    String path = await getDBPath();
    path += '/evidence.db';
    print(path);
    try {
      _instance = AppDatabase();
      await _instance!.init('$path');
    } catch (e) {
      print('database init error $e');
    }
    return _instance!;
  }

  ///数据库文件存储路径
  static Future<String> getDBPath()async{
    return await databaseFactory.getDatabasesPath() ;
    ///getApplicationSupportDirectory android '/data/user/0/com.t1559161567.jtd/files'
    ///getApplicationDocumentsDirectory android '/data/user/0/com.t1559161567.jtd/app_flutter'
    // return join((await getApplicationSupportDirectory()).path, DBConfig.dbName);
  }

  @override
  int get schemaVersion => 1;

  @override
  List<Table> get tables => [Offline33Table(), OfflineTable()];

  OfflineTable get offlineTable => tables[1] as OfflineTable;

  Offline33Table get Offline2Table => tables[0] as Offline33Table;
}

@DataClassName('Offline1')
class OfflineTable extends Table {
  ///无法使用常量如 EvidenceConstant.evidenceId，否则导致database.g.dart无法生成对应代码
  ///使用named，让自动生成的表字段 取该名称，而不是使用下标跟小写，代替大写，如evidence_id
  TextColumn get evidenceId => text().named('evidenceId')();

  TextColumn get offlineInfo => text().named("offlineInfo").nullable()();

  IntColumn get evidenceType => integer().named("evidenceType").nullable()();

  TextColumn get userId => text().named("userId").nullable()();

  TextColumn get orgId => text().named("orgId").nullable()();
  TextColumn get orgIdeee => text().named("orgIdeee").nullable()();

  ///不使用该重载方法，则会默认生成表名称 evidence_offline_info
  @override
  String get tableName => "evidenceOfflineInfo";

  @override
  Set<Column>? get primaryKey => {evidenceId};

  @override
  List<Column> get columns =>
      [evidenceId, offlineInfo, evidenceType, userId, orgId, orgIdeee];
}

@DataClassName('Offline2')
class Offline33Table extends Table {
  ///无法使用常量如 EvidenceConstant.evidenceId，否则导致database.g.dart无法生成对应代码
  ///使用named，让自动生成的表字段 取该名称，而不是使用下标跟小写，代替大写，如evidence_id
  TextColumn get evidenceId => text().named('evidenceId')();

  TextColumn get offlineInfo => text().named("offlineInfo").nullable()();

  IntColumn get evidenceType => integer().named("evidenceType").nullable()();

  TextColumn get userId => text().named("userId").nullable()();

  TextColumn get orgId => text().named("orgId").nullable()();

  ///不使用该重载方法，则会默认生成表名称 evidence_offline_info
  @override
  String get tableName => "evidenceOff";

  @override
  Set<Column>? get primaryKey => {evidenceId};

  @override
  List<Column> get columns =>
      [evidenceId, offlineInfo, evidenceType, userId, orgId];
}